package com.beiyou.service.impl;

import com.beiyou.controller.UserActivityServlet;
import com.beiyou.mapper.ActivityMapper;
import com.beiyou.mapper.UserMapper;
import com.beiyou.model.Activity;
import com.beiyou.model.User;
import com.beiyou.service.Aservice;
import com.beiyou.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 活动管理的代理类（实现类）
 */
public class ActivityServiceImpl implements Aservice {

    @Override
    public List<Activity> getAllActivity() {
        // 获取mybatis数据库会话对象
        SqlSession sqlSession = MybatisUtil.openSession();
        // 开启事务
        ActivityMapper activityMapper = sqlSession.getMapper(ActivityMapper.class);
        List<Activity> activityList = activityMapper.getAllActivity();
        // 提交事务
        sqlSession.commit();
        sqlSession.close();
        return activityList;
    }

    @Override
    public int addActivity(Activity activity) {
        // 获取mybatis数据库会话对象
        SqlSession sqlSession = MybatisUtil.openSession();
        // 开启事务
        ActivityMapper activityMapper = sqlSession.getMapper(ActivityMapper.class);
        int result = activityMapper.addActivity(activity);
        // 提交事务
        sqlSession.commit();
        sqlSession.close();
        return result;
    }

    @Override
    public int delActivity(String id) {
        // 获取mybatis数据库会话对象
        SqlSession sqlSession = MybatisUtil.openSession();
        // 开启事务
        ActivityMapper activityMapper = sqlSession.getMapper(ActivityMapper.class);
        int result = activityMapper.delActivity(id);
        // 提交事务
        sqlSession.commit();
        sqlSession.close();
        return result;
    }

    @Override
    public int updateActivity(Map<String, Object> activityMap) {
        // 获取mybatis数据库会话对象
        SqlSession sqlSession = MybatisUtil.openSession();
        // 开启事务
        ActivityMapper activityMapper = sqlSession.getMapper(ActivityMapper.class);
        int result = activityMapper.updateActivity(activityMap);
        // 提交事务
        sqlSession.commit();
        sqlSession.close();
        return result;
    }

    @Override
    public List<Activity> getActivityByCon(Activity activity) {
        // 获取mybatis数据库会话对象
        SqlSession sqlSession = MybatisUtil.openSession();
        // 开启事务
        ActivityMapper activityMapper = sqlSession.getMapper(ActivityMapper.class);
        List<Activity> activityList = activityMapper.getActivityByCon(activity);
        // 提交事务
        sqlSession.commit();
        sqlSession.close();
        return activityList;
    }

    /**
     *
     * 查询该用户参加的活动
     */
    @Override
    public List<Activity> getMyActivityByCon(User user){
        // 获取mybatis数据库会话对象
        SqlSession sqlSession = MybatisUtil.openSession();
        // 开启事务
        ActivityMapper activityMapper = sqlSession.getMapper(ActivityMapper.class);
        List<Activity> activityList = activityMapper.getMyActivityByCon(user);
        // 提交事务
        sqlSession.commit();
        sqlSession.close();
        return activityList;
    }

    /**
     * 某用户参加某活动
     * @param user
     * @param activity
     * @return
     */
    @Override
    public int joinActivity(User user, Activity activity) {
        // 获取mybatis数据库会话对象
        SqlSession sqlSession = MybatisUtil.openSession();
        // 开启事务
        ActivityMapper activityMapper = sqlSession.getMapper(ActivityMapper.class);
        Map<String,Object> params = new HashMap<>();
        params.put("user",user);
        params.put("activity",activity);
        int result = activityMapper.joinActivity(params);
        // 提交事务
        sqlSession.commit();
        sqlSession.close();
        return result;
    }
}
